package com.ctshk.rpc.system.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import javax.validation.Valid;

import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.system.dto.SupplierWriteOffUserDetailDTO;
import com.ctshk.rpc.system.entity.SupplierWriteOffUser;
import com.ctshk.rpc.system.mapper.SupplierWriteOffUserMapper;
import com.ctshk.rpc.system.req.AddSupplierWriteOffUserReq;
import com.ctshk.rpc.system.req.SupplierWriteOffUserQueryListReq;
import com.ctshk.rpc.system.service.ISupplierWriteOffUserService;

/**
 * <p>
 * 供应商核销员表 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-04-15
 */
@DubboService
public class SupplierWriteOffUserServiceImpl extends ServiceImpl<SupplierWriteOffUserMapper, SupplierWriteOffUser> implements ISupplierWriteOffUserService {

	@Autowired
    private SupplierWriteOffUserMapper supplierWriteOffUserMapper;
	
	/**新增供应商核销员
	 * @param req 入参
	 * @param id 登录用户id
	 * @return
	 */
	@Override
	public Result addSupplierWriteOffUser(AddSupplierWriteOffUserReq req, Long loginUserId) {
		SupplierWriteOffUser writeOffUser = EntityUtil.copy(req, SupplierWriteOffUser.class);
		writeOffUser.setId(SnowflakeIdWorker.nextId());
		writeOffUser.setCreateId(loginUserId);
		LocalDateTime now = LocalDateTime.now();
		writeOffUser.setGmtCreate(now);
		writeOffUser.setIsDeleted(IsDeletedCode.NO.getCode());
		supplierWriteOffUserMapper.insert(writeOffUser);
		return Result.success();
	}
	
	/**编辑供应商核销员
	 * @param req 入参
	 * @param id 登录用户id
	 * @return
	 */
	@Override
	public Result editSupplierWriteOffUser(@Valid AddSupplierWriteOffUserReq req, Long loginUserId) {
		SupplierWriteOffUser writeOffUser = EntityUtil.copy(req, SupplierWriteOffUser.class);
		writeOffUser.setModifiedId(loginUserId);
		writeOffUser.setGmtModified(LocalDateTime.now());
        int update = supplierWriteOffUserMapper.updateById(writeOffUser);
        if(update < 1){
            throw new BusinessException(SystemError.SYS_437);
        }
		
        return Result.success();
	}
	
	/**根据id查询供应商核销员详情
	 * @param id
	 * @return
	 */
	@Override
	public Result<SupplierWriteOffUserDetailDTO> queryDetail(Long id) {
		SupplierWriteOffUser writeOffUser = supplierWriteOffUserMapper.selectById(id);
		SupplierWriteOffUserDetailDTO dto = EntityUtil.copy(writeOffUser, SupplierWriteOffUserDetailDTO.class);
		return Result.success(dto);
	}
	
	/**供应商核销员列表查询
	 * @param req 查询条件
	 * @return
	 */
	@Override
	public PageResponse<SupplierWriteOffUserDetailDTO> queryList(SupplierWriteOffUserQueryListReq req) {
		IPage<SupplierWriteOffUser> page = new Page<>(req.getPageNo(),req.getPageSize());
        page = supplierWriteOffUserMapper.selectPage(page, null);
        //查询结果类型转换
        List<SupplierWriteOffUser> list = page.getRecords();
        List<SupplierWriteOffUserDetailDTO> resultList = EntityUtil.copyList(list, SupplierWriteOffUserDetailDTO.class);
        
        return  new PageResponse<>(resultList,page.getCurrent() == 1,
                page.getTotal() <= (page.getCurrent() * page.getSize()),
                page.getTotal(), page.getCurrent(), page.getSize());
	}
	
	/**删除核销员
	 * @param id
	 * @param id 登录用户id
	 * @return
	 */
	@Override
	public Result delete(Long id, Long loginUserId) {
		SupplierWriteOffUser writeOffUser = new SupplierWriteOffUser();
		writeOffUser.setId(id);
		writeOffUser.setIsDeleted(IsDeletedCode.YES.getCode());
		writeOffUser.setModifiedId(loginUserId);
		writeOffUser.setGmtModified(LocalDateTime.now());
		int rows = supplierWriteOffUserMapper.updateById(writeOffUser);
		if(rows < 1){
            throw new BusinessException(SystemError.SYS_437);
        }
		return Result.success();
	}

}
